﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Security;
using DefectTracker.Common;

namespace DefectTracker.WebSecurity
{
    public class DefectTrackerSqlMembershipProvider: MembershipProvider
    {
        MembershipProviderSettings m_MembershipProviderSettings;

        public DefectTrackerSqlMembershipProvider()
        {
            m_MembershipProviderSettings = RemotingHelper.Instance.Security.GetSettings();
        }

        public override string ApplicationName
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public override int MaxInvalidPasswordAttempts
        {
            get { return m_MembershipProviderSettings.MaxInvalidPasswordAttempts; }
        }

        public override int MinRequiredNonAlphanumericCharacters
        {
            get { return m_MembershipProviderSettings.MinRequiredNonAlphanumericCharacters; }
        }

        public override int MinRequiredPasswordLength
        {
            get { return m_MembershipProviderSettings.MinRequiredPasswordLength; }
        }

        public override int PasswordAttemptWindow
        {
            get { return m_MembershipProviderSettings.PasswordAttemptWindow; }
        }

        public override MembershipPasswordFormat PasswordFormat
        {
            get { return m_MembershipProviderSettings.PasswordFormat; }
        }

        public override string PasswordStrengthRegularExpression
        {
            get { return m_MembershipProviderSettings.PasswordStrengthRegularExpression; }
        }

        public override bool RequiresQuestionAndAnswer
        {
            get { return m_MembershipProviderSettings.RequiresQuestionAndAnswer; }
        }

        public override bool RequiresUniqueEmail
        {
            get { return m_MembershipProviderSettings.RequiresUniqueEmail; }
        }

        public override bool EnablePasswordReset
        {
            get { return m_MembershipProviderSettings.EnablePasswordReset; }
        }

        public override bool EnablePasswordRetrieval
        {
            get { return m_MembershipProviderSettings.EnablePasswordRetrieval; }
        }

        public override bool ChangePassword(string username, string oldPassword, string newPassword)
        {
            return RemotingHelper.Instance.Security.ChangePassword(username, oldPassword, newPassword);
        }

        public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)
        {
            return RemotingHelper.Instance.Security.ChangePasswordQuestionAndAnswer(username, password, newPasswordQuestion, newPasswordAnswer);
        }

        public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
        {
            CreateUserResponse createUserResponse;
            createUserResponse = RemotingHelper.Instance.Security.CreateUser(username, password, email, passwordQuestion, passwordAnswer, isApproved, providerUserKey);
            status = createUserResponse.Status;
            return createUserResponse.User;
        }

        public override bool DeleteUser(string username, bool deleteAllRelatedData)
        {
            return RemotingHelper.Instance.Security.DeleteUser(username, deleteAllRelatedData);
        }

        public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            FindUserResponse findUserResponse;
            findUserResponse = RemotingHelper.Instance.Security.FindUsersByEmail(emailToMatch, pageIndex, pageSize);
            totalRecords = findUserResponse.TotalRecords;
            return findUserResponse.MembershipUserCollection;
        }

        public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            FindUserResponse findUserResponse;
            findUserResponse = RemotingHelper.Instance.Security.FindUsersByName(usernameToMatch, pageIndex, pageSize);
            totalRecords = findUserResponse.TotalRecords;
            return findUserResponse.MembershipUserCollection;
        }

        public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
        {
            FindUserResponse findUserResponse;
            findUserResponse = RemotingHelper.Instance.Security.GetAllUsers(pageIndex, pageSize);
            totalRecords = findUserResponse.TotalRecords;
            return findUserResponse.MembershipUserCollection;
        }

        public override int GetNumberOfUsersOnline()
        {
            return RemotingHelper.Instance.Security.GetNumberOfUsersOnline();
        }

        public override string GetPassword(string username, string answer)
        {
            return RemotingHelper.Instance.Security.GetPassword(username, answer);
        }

        public override MembershipUser GetUser(string username, bool userIsOnline)
        {
            return RemotingHelper.Instance.Security.GetUser(username, userIsOnline);
        }

        public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
        {
            return RemotingHelper.Instance.Security.GetUser(providerUserKey, userIsOnline);
        }

        public override string GetUserNameByEmail(string email)
        {
            return RemotingHelper.Instance.Security.GetUserNameByEmail(email);
        }

        public override string ResetPassword(string username, string answer)
        {
            return RemotingHelper.Instance.Security.ResetPassword(username, answer);
        }

        public override bool UnlockUser(string userName)
        {
            return RemotingHelper.Instance.Security.UnlockUser(userName);
        }

        public override void UpdateUser(MembershipUser user)
        {
            RemotingHelper.Instance.Security.UpdateUser(user);
        }

        public override bool ValidateUser(string username, string password)
        {
            return RemotingHelper.Instance.Security.ValidateUser(username, password);
        }
    }
}
